//
//  TBCReadingWebViewController.m
//  待定
//
//  Created by BoyaLee on 15/11/5.
//  Copyright © 2015年 BoyaLee. All rights reserved.
//

#import "TBCReadingWebViewController.h"

@interface TBCReadingWebViewController () <UIWebViewDelegate>

@property (strong, nonatomic) UIWebView *readingWebView;

@end

@implementation TBCReadingWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initReadingWebViews];
    [self initReadingWebData];
    
    UIImage *image = [UIImage imageNamed:@"fanhui.png"];
    //取消渲染
    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(leftBarAction:)];
    
}


//自定义左侧按钮
-(void)leftBarAction:(UIBarButtonItem *)barItem
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)initReadingWebViews {
    
    //self.view.backgroundColor = [UIColor whiteColor];
    
    self.readingWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    self.readingWebView.backgroundColor = [UIColor clearColor];
    self.readingWebView.scrollView.backgroundColor = [UIColor clearColor];
    self.readingWebView.scrollView.showsVerticalScrollIndicator = NO;
    self.readingWebView.scrollView.showsHorizontalScrollIndicator = NO;
    self.readingWebView.delegate = self;
    [self.view addSubview:self.readingWebView];
    
}

- (void)initReadingWebData {
    
    NSString *url = @"http://api2.pianke.me/article/info";
    NSDictionary *parameters = @{@"auth":@"C807FCuwm36lDC8l3HMTslDJxQMKpNLeNoj1vCGMzcWP22NZLTRhY4fI",@"client":@"1",@"contentid":self.contentID,@"deviceid":@"63900960-1ED5-47D0-B561-86C09F41ED08",@"version":@"3.0.6"};
    NSString *cookie = @"PHPSESSID=1t0dhk4gtdibh9r2ce80dolpr1";
    
    [self getDataFromAFN:url Parameters:parameters Cookie:cookie];
    
}

#pragma mark -- 请求数据
- (void)getDataFromAFN:(NSString *)url Parameters:(NSDictionary *)parameters Cookie:(NSString *)cookie {
    
#pragma mark -- 判断是否有网络
    
    NSInteger result = [TBCAppTools getInternetReachability:url];
    
    if (result >= InternetReachableWithWWAN) {
        
        [TBCAppTools postLocalDataWithURL:url Parameters:parameters Cookie:cookie TBCToolsBlock:^(id responseObject) {
            
            NSDictionary *dic = responseObject;
            NSString *html = [[dic objectForKey:@"data"] objectForKey:@"html"];
            
            [self.readingWebView loadHTMLString:html baseURL:nil];
            [self.view bringSubviewToFront:self.readingWebView];
            
        }];
        
    }
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
   
#pragma mark -- 修改webview图片大小
    // 拦截网页图片，并修改图片大小
    [webView stringByEvaluatingJavaScriptFromString:
     @"var script = document.createElement('script');"
     "script.type = 'text/javascript';"
     "script.text = \"function ResizeImages() { "
        "var myimg,oldwidth;"
        "var maxwidth=320;"// 缩放系数
        "for(i=0;i <document.images.length;i++){"
            "myimg = document.images[i];"
            "if(myimg.width > maxwidth){"
                "oldwidth = myimg.width;"
                "myimg.width = maxwidth;"
                "myimg.height = myimg.height * (maxwidth/oldwidth);"
            "}"
        "}"
     "}\";"
     "document.getElementsByTagName('head')[0].appendChild(script);"];
    
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
